package com.collect.cameroon.zhiScene.zhiWidget.zhiSubmitRecTagView;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.collect.cameroon.R;
import com.collect.cameroon.zhiBean.zhiSend.ZHISMarkCustomerFollowUser;
import com.collect.cameroon.zhiScene.zhiWidget.zhiStepView.ZHINextStepView;
import com.collect.cameroon.zhiUtils.StringUtil;

public class ZHISubmitRecRecallView extends PopupWindow {

    private View mMenuView;
    private TextView tv_name;
    private TextView tv_contact;
    private EditText et_remarks;
    private ZHINextStepView btn_negtive;

    private OnSubmitRecRecallItemClickListener mOnSubmitRecRecallItemClickListener;
    private Context mContext;
    private String mOrderId;
    private String mType;
    private String mPhone;

    @SuppressLint("ClickableViewAccessibility")
    public ZHISubmitRecRecallView(Context context) {
        super(context);
        mContext = context;

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.popview_record_recall, null);
        tv_name = mMenuView.findViewById(R.id.tv_name);
        tv_contact = mMenuView.findViewById(R.id.tv_contact);
        et_remarks = mMenuView.findViewById(R.id.et_remarks);
        btn_negtive = mMenuView.findViewById(R.id.btn_negtive);

        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.PopWindowFromBottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xa0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
//        this.setClippingEnabled(false);

        btn_negtive.setOnMenuItemDidChangedListener(item -> {
            if (mOnSubmitRecRecallItemClickListener != null) {
                mOnSubmitRecRecallItemClickListener.onSubmitClick(fillRecParamsV2());
            }
        });

        mMenuView.setOnTouchListener((v, event) -> {
            int height = mMenuView.findViewById(R.id.ll_pop_layout).getTop();
            int y = (int) event.getY();
            if (event.getAction() == MotionEvent.ACTION_UP) {
                if (y < height) {
                    dismiss();
                }
            }
            return true;
        });
    }

    public void setName(String name) {
        try {
            tv_name.setText(name);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setContact(String contact) {
        try {
            mPhone = StringUtil.safeString(contact);
            String contactEncryptFormatter = StringUtil.phoneNumberEncryptFormatter(contact);
            tv_contact.setText(contactEncryptFormatter);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setRemarks(String remark) {
        et_remarks.setText(StringUtil.safeString(remark));
    }

    public ZHISMarkCustomerFollowUser fillRecParamsV2() {
        ZHISMarkCustomerFollowUser zhisMarkCustomerFollowUser = new ZHISMarkCustomerFollowUser();
        zhisMarkCustomerFollowUser.setMobile(mPhone);
        zhisMarkCustomerFollowUser.setProduct(tv_name.getText().toString().trim());
        zhisMarkCustomerFollowUser.setRemark(et_remarks.getText().toString().trim());
        return zhisMarkCustomerFollowUser;
    }

    public void setOnSubmitRecRecallItemClickListener(OnSubmitRecRecallItemClickListener submitRecTagItemClickListener) {
        this.mOnSubmitRecRecallItemClickListener = submitRecTagItemClickListener;
    }

    public interface OnSubmitRecRecallItemClickListener {
        void onSubmitClick(ZHISMarkCustomerFollowUser zhisMarkCustomerFollowUser);
    }
}
